//
//  JZWebViewController.m
//  tf02
//
//  Created by Jim on 2017/3/14.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "JZWebViewController.h"

#import "JZh5View.h"

#import "JZGoBackView.h"

@interface JZWebViewController () <JZh5ViewDelegate>

@property (nonatomic, strong, readwrite) JZh5View *h5View;

@property (nonatomic, strong) JZGoBackView *goBackView;

@property (nonatomic, copy, readwrite) NSString *URLString;

/**
 * 返回上一级并改变返回按键的UI界面
 */
- (void)private_returnToLastPageAndChangeUI;

/**
 * 返回上一级
 */
- (void)private_returnToLastPage;

/**
 * 关闭网页
 */
- (void)private_closeWebPage;

@end

@implementation JZWebViewController

#pragma mark - JZBrowerWebViewController 初始化方法

- (instancetype)initWithURLString:(NSString *)URLStirng title:(NSString *)title
{
    if (self = [super init]) {
        
        self.title = title;
    
        _URLString = URLStirng;
    }
    return self;
}

+ (instancetype)webViewControllerWithURLString:(NSString *)URLString title:(NSString *)title
{
    return [[self alloc] initWithURLString:URLString title:title];
}

#pragma mark - 懒加载

- (JZGoBackView *)goBackView
{
    if (!_goBackView) {
        _goBackView = [[JZGoBackView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    }
    return _goBackView;
}

- (JZh5View *)h5View
{
    if (!_h5View) {
        _h5View = [[JZh5View alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - APP_NAVIGATION_HEIGHT - APP_STATUSBAR_HEIGHT)];
        
        _h5View.URLString = _URLString;
        
        _h5View.delegate = self;

    }
    return _h5View;
}

#pragma mark - UIViewController 系统方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.goBackView.closeBtn.hidden = YES;
    
    [self.goBackView.backBtn addTarget:self action:@selector(private_returnToLastPageAndChangeUI) forControlEvents:UIControlEventTouchUpInside];
    
    [self.goBackView.closeBtn addTarget:self action:@selector(private_closeWebPage) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.goBackView];
    
    self.navigationItem.leftBarButtonItem = backBarButtonItem;
    
    [self.view addSubview:self.h5View];
    
//    [self.view addSubview:self.safariBtn];
    
}

#pragma mark - JZWebViewController 私有方法

/**
 * 返回上一级并改变返回按键的UI界面
 */
- (void)private_returnToLastPageAndChangeUI
{
    NSLog(@"private_returnToLastPageAndChangeUI");
    
    if (self.h5View.webView.canGoBack) {
        
        [self.h5View.webView goBack];
        
        [self.goBackView.backBtn removeTarget:self action:@selector(private_returnToLastPageAndChangeUI) forControlEvents:UIControlEventTouchUpInside];
        
        [self.goBackView.backBtn addTarget:self action:@selector(private_returnToLastPage) forControlEvents:UIControlEventTouchUpInside];
        
        self.goBackView.closeBtn.hidden = NO;
                
    } else {
        
        NSLog(@"NSThread.currentThread = %@", [NSThread currentThread]);
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/**
 * 返回上一级
 */
- (void)private_returnToLastPage
{
    NSLog(@"private_returnToLastPage");
    
    if (self.h5View.webView.canGoBack) {
        
        [self.h5View.webView goBack];
        
    } else {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/**
 * 关闭网页
 */
- (void)private_closeWebPage
{
    NSLog(@"private_closeWebPage");
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - JZh5ViewDelegate 代理方法
- (void)h5View:(JZh5View *)h5View updateNewWebPageWithURL:(NSString *)URLString
{
    _URLString = URLString;
    
}

- (void)h5View:(JZh5View *)h5View didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"页面开始加载时发生错误 = %@", error);
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"页面开始加载时发生错误" preferredStyle:UIAlertControllerStyleAlert];
//    
//    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [self.navigationController popViewControllerAnimated:YES];
//    }];
//    
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"不管" style:UIAlertActionStyleCancel handler:nil];
//    
//    [alertController addAction:okAction];
//    
//    [alertController addAction:cancelAction];
//    
//    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)h5View:(JZh5View *)h5View didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"页面提交时发生错误 = %@", error);
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"页面提交时发生错误" preferredStyle:UIAlertControllerStyleAlert];
//    
//    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [self.navigationController popViewControllerAnimated:YES];
//    }];
//    
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"不管" style:UIAlertActionStyleCancel handler:nil];
//    
//    [alertController addAction:okAction];
//    
//    [alertController addAction:cancelAction];
//    
//    [self presentViewController:alertController animated:YES completion:nil];
}

@end
